package com.shangdiao.sd_server.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RequestRenderUtil {

  static Logger logger = LoggerFactory.getLogger(RequestRenderUtil.class);

	public static void setHeader(HttpServletResponse response) {
		response.setContentType("text/html;charset=utf-8");
	}

	/**
	 * 返回 JsonP格式的响应结果
	 * 
	 * @param text
	 * @param callback
	 * @param encode
	 */
	public static void renderJsonPText(HttpServletResponse response, String text,
			String callback, String encode) {
		// 这里不加的话，AJAX会缓存每次取的数据，从而造成数据不能更新
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		setHeader(response);
		try {
			text = callback + "(" + text + ")";
			response.getWriter().write(text);
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	public static void renderText(HttpServletResponse response, String text) {
		// 这里不加的话，AJAX会缓存每次取的数据，从而造成数据不能更新
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		setHeader(response);
		try {
			response.getWriter().write(text);
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	public static void renderText(HttpServletResponse response, String text,
			String encode) {
		// 这里不加的话，AJAX会缓存每次取的数据，从而造成数据不能更新
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("text/html;charset=" + encode);
		try {
			response.getWriter().write(text);
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	public static void renderPureText(HttpServletResponse response, String text,
			String encode) {
		response.setContentType("text/html;charset=" + encode);
		try {
			response.getWriter().write(text);
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	/**
	 * 获得前一页面
	 * 
	 * @return
	 */
	public static String getReferer(HttpServletRequest request) {
		return request.getHeader("REFERER");
	}

	/** 页面重定向 */
	public static String sendRedirect(HttpServletResponse response, String url) {
		try {
			response.sendRedirect(url);
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return null;
	}

	/**
	 * 弹窗
	 * 
	 * @param response
	 * @param content
	 * @param url
	 * @throws Exception
	 */
	public static void renderScriptText(HttpServletResponse response, String content,
			String url) {
		String text = "<script>alert('" + content + "');window.location.href='"
				+ url + "';</script>";
		renderText(response, text);
	}

	public static void renderScriptRedirect(HttpServletResponse response, String url) {
		String text = "<script type='text/javascript'>window.location.href='"
				+ url + "';</script>";
		renderText(response, text);
	}

	/**
	 * 弹窗
	 * 
	 * @param response
	 * @param content
	 * @param url
	 * @throws Exception
	 */
	public static void renderParentJs(HttpServletResponse response, String content,
			String url) {
		String text = "<script>alert('" + content
				+ "');parent.window.location.href='" + url + "';</script>";
		renderText(response, text);
	}

	/**
	 * 输出JS代码
	 * 
	 * @param response
	 * @param content
	 * @param url
	 * @throws Exception
	 */
	public static void renderScript(HttpServletResponse response, String jscode) {
		String text = "<script>" + jscode + "</script>";
		renderText(response, text);
	}

	/**
	 * 子窗口返回数据，把值传给父窗口后，关闭子窗口
	 * 
	 * @param response
	 * @param content
	 * @param url
	 * @throws Exception
	 */
	public static void renderScriptText(HttpServletResponse response, String content) {
		renderText(
				response,
				"<script>document.write('<div style=\"font-size:12px;text-align:center;color:red\">"
						+ content
						+ "<br><input type=\"button\" value=\"确定并关闭窗口\" onclick=\"parent.location.href=parent.location.href;parent.Dialog.close();\"></div>');</script>");
	}

	/**
	 * 弹出提示框后返回上一页面并刷新（保证了参数与第一次进入上一页面相同）
	 * 
	 * @param content
	 */
	public static void alert(HttpServletResponse response, String content) {
		String text = "<script>alert('" + content
				+ "');window.history.back();location.reload();</script>";
		renderText(response, text);
	}

	/**
	 * 清楚缓存设置
	 * */
	public static void cleanCache(HttpServletResponse response) {
		// 清缓存
		response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
		response.setHeader("Pragma", "no-cache"); // HTTP 1.0
		response.setDateHeader("Expires", 0); // prevents caching at the proxy
	}

}
